package org.foj.dao.impl;

import org.appfuse.dao.hibernate.GenericDaoHibernate;
import org.foj.dao.IssueDao;
import org.foj.model.Issue;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.Conjunction;

import java.util.List;

/**
 * @author jeremy anderson
 */
public class IssueDaoImpl extends GenericDaoHibernate<Issue, Long> implements IssueDao {

    public IssueDaoImpl() {
        super(Issue.class);
    }

    @SuppressWarnings("unchecked")
    public List<Issue> findByCriteria(String projectName, String issueType, String severity, String status) {
        Conjunction conjunction = Restrictions.conjunction();

        if (projectName != null && !projectName.trim().equals("")) {
            conjunction.add(Restrictions.eq("project", projectName));
        }

        if (issueType != null && !issueType.trim().equals("")) {
            conjunction.add(Restrictions.eq("type", issueType));
        }

        if (severity != null && !severity.trim().equals("")) {
            conjunction.add(Restrictions.eq("severity", severity));
        }

        if (status != null && !status.trim().equals("")) {
            conjunction.add(Restrictions.eq("status", status));
        }

        return getSession().createCriteria(Issue.class).add(conjunction).list();
    }

}
